﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using mscook.mvc.Filters;
using mscook.mvc.Models;
using mscook.core.services;
using mscook.mvc.Core;
using mscook.core.domain;
using System.Security.Principal;
using mscook.mvc.core;
using mscook.core.repositories;
using mscook.core;
namespace mscook.mvc.Controllers
{
    public class ChefController : BaseController
    {
        //
        // GET: /Chef/
        private ILoginUserService iLoginUserService;
        private ICookBookRepository iCookBookRepository = UnityHelper.UnityToT<ICookBookRepository>();
        private IChefService iChefService;
        public ChefController(IActionBonusSettingService actionBonusSettingService, IResourceBonusSettingService resourceBonusSettingService, ILoginUserService loginUserService, IChefService iChefService)
            : base(actionBonusSettingService, resourceBonusSettingService, loginUserService)
        {
            this.iLoginUserService = loginUserService;
            this.iChefService = iChefService;
        }
        public ActionResult Index(string[] resourceTypes, string num = "",string keyword="")
        {
            ChefModel chefModel = new ChefModel();
            long total = 0;
            PageBreakHelper pageBreakHelper = new PageBreakHelper();
            pageBreakHelper.Init(num, "12");

            //chefModel.ChefList = iChefService.SelectPageList(pageBreakHelper.Start,pageBreakHelper.PerValue,out total);
            if (resourceTypes != null)
            {
                IList<string> resoureTypeList = resourceTypes.Where(o => !string.IsNullOrEmpty(o)).ToList();
                //cookBookModel.CookBookList = iCookBookService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                if (resoureTypeList.Count == 0)
                {
                    chefModel.ChefList = GetByKeywordOrPage(pageBreakHelper, keyword, out total);
                }
                else
                {
                    chefModel.ChefList = iChefService.SelectLimitbyIds(GetRsourceIdsByTypes(resoureTypeList, PurchasedCategory.Chef), pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                    chefModel.SelectResourceTypes = resoureTypeList;
                }
            }
            else
            {
                chefModel.ChefList = GetByKeywordOrPage(pageBreakHelper,keyword,out total);

            }


            ViewData["Keyword"] = keyword;
            pageBreakHelper.SetViewDataKeys(ViewData, ViewBag, total);
            InitSourceType(chefModel, PurchasedCategory.Chef);
            InitNavBar();
            return View(chefModel);
        }

        [ActionBonusFilter]
        public ActionResult ReadChef(string id)
        {

            Chef chef = iChefService.Get(id);
            if (chef != null)
            {
                ReadChefModel readChefModel = new ReadChefModel() { Chef = chef };
                readChefModel.CookBooks = iCookBookRepository.GetByChef(chef, 0, 10);
                readChefModel.LoginUser = CurrentUser;
                readChefModel.Magazine = GetPushMagazine();
                InitNavBar();
                return View(readChefModel);
            }
            // ViewBag.CookBookList = iCookBookRepository.GetByChef(chef,0,10);

            return RedirectToAction("index");
        }

        private IList<Chef> GetByKeywordOrPage(PageBreakHelper pageBreakHelper,string keyword,out long total)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return iChefService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
            }
            else
            {
               return  iChefService.GetByKeyword(keyword, pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
            }
        }

    }
}
